const api = require('../../network/api');
const { default: cartoon } = require('../../network/api-data/cartoon');


Page({

    /**
     * 页面的初始数据
     */
    data: {
        cartoon: {},
        chapters: [],
        active: 'second',
        update_time: "",
        chapter_id: '',
        scrollTop: 0,
        chpaterFlag: false,
        id: 0


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(option) {
        const { id } = option
        if (id) {
            this.init(id)
            this.setData({
                id: id
            })
        }

    },
    onShow() {
        // this.init(7)

    },
    lower() {

    },
    onChapterDetail(e) {
        console.log(e.currentTarget.dataset.id);
        const id = e.currentTarget.dataset.id
        const cartoon = this.data.cartoon
        wx.navigateTo({
            url: `/pages/novel-chapter-page/index?id=${id}&comic_id=${cartoon.id}`
        })
    },
    scroll(e) {
        // console.log(e,'scroll')
    },
    scrollToTop() {
        this.setData({
            scrollTop: 0
        })
    },
    async init(id) {
        console.log(id, '====>');
        const user = wx.getStorageSync('userInfo')
        try {
            wx.showLoading({
                title: '数据加载中',
            })
            const res = await api.novelDetail({ id })
            const res1 = await api.getNovelChapter({ comic_id: id, isAll: true })
            const param = {
                    uid: user.id,
                    comic_id: id
                }
                // const res2 = await api.historicalRecord(param)
            console.log(res1.data, '阅读记录');
            if (res.code == 200 && res1.code == 200) {
                let length = res1.data.data.length - 1
                let chapter_id = res1.data.data[0].id
                let chpaterFlag = false
                    // if (res2.data.length > 0) {
                    //     chapter_id = res2.data[0].chapter_id
                    //     chpaterFlag = true

                // }
                let update_time = res1.data.data[length].create_time.replace('T', ' ').replace('.000Z', '')
                wx.setNavigationBarTitle({
                    title: res.data.name,
                })
                this.setData({
                    cartoon: res.data,
                    chapters: res1.data.data,
                    update_time,
                    chapter_id,
                    chpaterFlag
                })
            }
            wx.hideLoading()

        } catch (error) {
            console.log(error);
            wx.hideLoading()

        }
    },
    onChange(event) {
        console.log(event, 'event');
    },
    sortArr() {
        let chapter = this.data.chapters.reverse()
        this.setData({
            chapters: chapter
        })
    }
})